package com.lex.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lex.common.api.CommonResult;
import com.lex.data.admin.mapper.LexSysLogMapper;
import com.lex.data.admin.mapper.LexSysLoginLogMapper;
import com.lex.data.admin.model.LexSysLog;
import com.lex.data.admin.model.LexSysLoginLog;
import com.lex.entity.params.LoginLogQueryParams;
import com.lex.entity.params.SysLogQueryParams;
import com.lex.service.LogService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LexSysLogMapper lexSysLogMapper;

    @Autowired
    private LexSysLoginLogMapper lexSysLoginLogMapper;
    /**
     * 查询操作日志列表，带分页
     * @param query
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult getOperationLogList(SysLogQueryParams query, Integer page, Integer pageSize) {
        Page<LexSysLog> sysLogPage = new Page<>(page, pageSize);
        QueryWrapper<LexSysLog> wrapper = getLexSysLogQueryWrapper(query);
        List<LexSysLog> lexSysLogs = lexSysLogMapper.selectList(sysLogPage, wrapper);
        long total = sysLogPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", lexSysLogs);
        return CommonResult.success(map);
    }

    private static QueryWrapper<LexSysLog> getLexSysLogQueryWrapper(SysLogQueryParams query) {
        QueryWrapper<LexSysLog> wrapper = new QueryWrapper<>();
        if (query.getTitle() != null) {
            //模块
            wrapper.like("title", query.getTitle());
        }
        if (query.getOperName() != null) {
            //操作人员
            wrapper.like("oper_name", query.getOperName());
        }
        if (query.getBusinessType() != null) {
            //类型
            wrapper.eq("business_type", query.getBusinessType());
        }
        if (query.getStatus() != null) {
            //状态
            wrapper.eq("status", query.getStatus());
        }
        if (query.getStartTime() != null && query.getEndTime() == null) {
            String start_time = query.getStartTime();
            //将start_time字符串 转换为时间戳
            Timestamp timestamp = Timestamp.valueOf(start_time);
            //开始时间
            wrapper.ge("oper_time", timestamp);
        } else if (query.getStartTime() == null && query.getEndTime() != null) {
            String end_time = query.getEndTime();
            //将end_time字符串 转换为时间戳
            Timestamp timestamp = Timestamp.valueOf(end_time);
            //结束时间
            wrapper.le("oper_time", timestamp);
        } else if (query.getStartTime() != null) {
            String start_time = query.getStartTime();
            String end_time = query.getEndTime();
            //将start_time字符串 转换为时间戳
            Timestamp start_timestamp = Timestamp.valueOf(start_time);
            Timestamp end_timestamp = Timestamp.valueOf(end_time);
            //开始时间和结束时间
            wrapper.between("oper_time", start_timestamp, end_timestamp);
        }
        wrapper.orderByDesc("oper_time");
        return wrapper;
    }

    /**
     * 删除日志，等保要求日志要求保留6个月以上，默认删除6个月之前的日志
     * @return
     */
    @Override
    public CommonResult deleteOperationLog() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime sixMonthsAgo = now.minusMonths(6);
        Timestamp timestamp = Timestamp.valueOf(sixMonthsAgo);
        int result = lexSysLogMapper.delete(
                new QueryWrapper<LexSysLog>()
                        .lt("oper_time", timestamp));
        String message = "总计删除了" + result + "条数据！";
        return CommonResult.success(null,message);
    }
    /*
        * 查询登录日志列表
     */
    @Override
    public CommonResult getLoginLogList(LoginLogQueryParams query, Integer page, Integer pageSize) {
        Page<LexSysLoginLog> loginLogPage = new Page<>(page, pageSize);
        QueryWrapper<LexSysLoginLog> wrapper = new QueryWrapper<>();
        if(query.getUserName() != null){
            wrapper.like("user_name", query.getUserName());
        }
        if (query.getStatus() != null) {
            wrapper.eq("status", query.getStatus());
        }
        if (query.getStartTime() != null && query.getEndTime() == null) {
            String start_time = query.getStartTime();
            //将start_time字符串 转换为时间戳
            Timestamp timestamp = Timestamp.valueOf(start_time);
            //开始时间
            wrapper.ge("login_time", timestamp);
        } else if (query.getStartTime() == null && query.getEndTime() != null) {
            String end_time = query.getEndTime();
            //将end_time字符串 转换为时间戳
            Timestamp timestamp = Timestamp.valueOf(end_time);
            //结束时间
            wrapper.le("login_time", timestamp);
        } else if (query.getStartTime() != null) {
            String start_time = query.getStartTime();
            String end_time = query.getEndTime();
            //将start_time字符串 转换为时间戳
            Timestamp start_timestamp = Timestamp.valueOf(start_time);
            Timestamp end_timestamp = Timestamp.valueOf(end_time);
            //开始时间和结束时间
            wrapper.between("login_time", start_timestamp, end_timestamp);
        }
        wrapper.orderByDesc("login_time");
        List<LexSysLoginLog> lexSysLoginLogs = lexSysLoginLogMapper.selectList(loginLogPage, wrapper);
        long total = loginLogPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", lexSysLoginLogs);
        return CommonResult.success(map);
    }

    /**
     * 删除登录日志，等保要求日志要求保留6个月以上，默认删除6个月之前的日志
     * @return
     */
    @Override
    public CommonResult deleteLoginLog() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime sixMonthsAgo = now.minusMonths(6);
        Timestamp timestamp = Timestamp.valueOf(sixMonthsAgo);
        int result = lexSysLoginLogMapper.delete(
                new QueryWrapper<LexSysLoginLog>()
                        .lt("login_time", timestamp));
        String message = "总计删除了" + result + "条数据！";
        return CommonResult.success(null,message);
    }
}
